#include "B_TableViewModel.h"

#include <QDebug>
#include <QIcon>
#include <QDate>
B_TableViewModel::B_TableViewModel(QObject* parent)
    : QAbstractTableModel{parent}
{
    /** 表头名称 */
    _header << "书名"
            << "借出时间"
            << "应还日期"
            << "剩余天数";


    /** 测试数据 */

    // 未过期
    QStringList data0;
    data0 << "百年孤独"
          << "2024-03-10"
          << QDate::fromString("2024-03-10", "yyyy-MM-dd").addDays(30).toString("yyyy-MM-dd")
          << QString::number(QDate::currentDate().daysTo(QDate(2024,4,9)));

    // 即将到期
    QStringList data1;
    data1 << "人类简史"
          << "2024-03-25"
          << QDate(2024,3,25).addDays(30).toString("yyyy-MM-dd")
          << QString::number(QDate::currentDate().daysTo(QDate(2024,4,24)));

    // 已续借
    QStringList data2;
    data2 << "代码大全"
          << "2024-02-15"
          << QDate(2024,2,15).addDays(45).toString("yyyy-MM-dd")  // 续借15天
          << QString::number(QDate::currentDate().daysTo(QDate(2024,4,1)));

    // 超期图书
    QStringList data3;
    data3 << "设计心理学"
          << "2024-02-28"
          << QDate(2024,3,29).toString("yyyy-MM-dd")  // 应还日期已过
          << QString::number(QDate::currentDate().daysTo(QDate(2024,3,29)));  // 显示负数

    // 多本借阅
    QStringList data4;
    data4 << "算法导论"
          << "2024-04-01"
          << QDate(2024,5,1).toString("yyyy-MM-dd")
          << QString::number(QDate::currentDate().daysTo(QDate(2024,5,1)));

    // 新近借阅
    QStringList data5;
    data5 << "失控"
          << QDate::currentDate().addDays(-2).toString("yyyy-MM-dd")  // 2天前借出
          << QDate::currentDate().addDays(28).toString("yyyy-MM-dd")  // 剩余28天
          << "28";

    // 长期借阅
    QStringList data6;
    data6 << "数据库系统概念"
          << "2023-12-01"
          << QDate(2024,6,30).toString("yyyy-MM-dd")  // 长期借阅（学术书籍）
          << QString::number(QDate::currentDate().daysTo(QDate(2024,6,30)));

    // 跨月计算
    QStringList data7;
    data7 << "Qt编程指南"
          << "2024-03-31"
          << QDate(2024,4,30).toString("yyyy-MM-dd")  // 正好30天
          << QString::number(QDate::currentDate().daysTo(QDate(2024,4,30)));


    _dataList.append(data0);
    _dataList.append(data1);
    _dataList.append(data2);
    _dataList.append(data3);
    _dataList.append(data4);
    _dataList.append(data5);
    _dataList.append(data6);
    _dataList.append(data7);

    for(QStringList &itme: _dataList)
    {
        itme.append(QString("测试数据"));
    }

    _iconList.append(QIcon(QPixmap(":/Resource/Image/Model/NaightNavigationStar.jpg").scaled(38, 38, Qt::KeepAspectRatio, Qt::SmoothTransformation)));
    _iconList.append(QIcon(QPixmap(":/Resource/Image/Model/MaVieEnRose.jpg").scaled(38, 38, Qt::KeepAspectRatio, Qt::SmoothTransformation)));
    _iconList.append(QIcon(QPixmap(":/Resource/Image/Model/CollapsingWorld.jpg").scaled(38, 38, Qt::KeepAspectRatio, Qt::SmoothTransformation)));
    _iconList.append(QIcon(QPixmap(":/Resource/Image/Model/RainMan.jpg").scaled(38, 38, Qt::KeepAspectRatio, Qt::SmoothTransformation)));
    _iconList.append(QIcon(QPixmap(":/Resource/Image/Model/DarkForest.jpg").scaled(38, 38, Qt::KeepAspectRatio, Qt::SmoothTransformation)));
    _iconList.append(QIcon(QPixmap(":/Resource/Image/Model/Light.jpg").scaled(38, 38, Qt::KeepAspectRatio, Qt::SmoothTransformation)));
    _iconList.append(QIcon(QPixmap(":/Resource/Image/Model/STYXHELIX.jpg").scaled(38, 38, Qt::KeepAspectRatio, Qt::SmoothTransformation)));
    _iconList.append(QIcon(QPixmap(":/Resource/Image/Model/LASTSTARDUST.jpg").scaled(38, 38, Qt::KeepAspectRatio, Qt::SmoothTransformation)));
    _iconList.append(QIcon(QPixmap(":/Resource/Image/Model/RunningInTheDark.jpg").scaled(38, 38, Qt::KeepAspectRatio, Qt::SmoothTransformation)));
}

B_TableViewModel::~B_TableViewModel()
{
}

int B_TableViewModel::rowCount(const QModelIndex& parent) const
{
    return _dataList.count();
}

int B_TableViewModel::columnCount(const QModelIndex& parent) const
{
    return _header.count();
}

QVariant B_TableViewModel::data(const QModelIndex& index, int role) const
{
    int row = _dataList.count();
    // int column = _header.count();
    if (role == Qt::DisplayRole)
    {
        // 列0返回文本
        if (index.column() == 0) {
            return _dataList[index.row() % row][0];
        }
/*
        qDebug() << QString("_dataList[%1][%2]:%3")
                        .arg(index.row())
                        .arg(index.column() - 1)
                        .arg(_dataList[index.row()][index.column() - 1]);
*/
        return _dataList[index.row() % row][index.column()];
    }/*
    else if (role == Qt::DecorationRole && index.column() == 0)
    {
        // return _iconList[index.row() % row];
        // return "<书本封面>";
        return QIcon(QPixmap(":/resource/Icon/table_book.png").scaled(38, 38, Qt::KeepAspectRatio, Qt::SmoothTransformation));
    }*/
    else if (role == Qt::DecorationPropertyRole)
    {
        return Qt::AlignCenter;
    }
    else if (role == Qt::TextAlignmentRole /*&& index.column() == 4*/)
    {
        return Qt::AlignCenter;
    }
    return QVariant();
}

QVariant B_TableViewModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
    {
        return _header[section];
    }
    return QAbstractTableModel::headerData(section, orientation, role);
}
